/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vadung.contragame2d;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author dung.voanh
 */
public class GameTiledLayer {

    protected int m_iImageID;
    protected Image m_imgTileset;
    protected String m_strNameType;
    protected int m_aiDataMap[];
    protected int m_aiTiledX[];
    protected int m_aiTiledY[];
    protected int m_iTiledWidth;
    protected int m_iTiledHeight;
    protected int m_iRowsMap;
    protected int m_iColumnsMap;

    public void SetNameType(String strNameType) {
        this.m_strNameType = strNameType;
    }
    
    public int GetDaTa(int row, int column){
        if(row < 0 || row >= m_iRowsMap || column < 0 || column >= m_iColumnsMap)
            return -1;
        int index = row * m_iColumnsMap + column;
        return m_aiDataMap[index];
    }
    
    public GameTiledLayer() {

    }

    public void Load(Image imgTileset, int tiledWidth, int tiledHeight, int aiDataMap[], int rowsMap, int columnsMap) {
        m_imgTileset = imgTileset;
        m_iTiledWidth = tiledWidth;
        m_iTiledHeight = tiledHeight;
        m_iRowsMap = rowsMap;
        m_iColumnsMap = columnsMap;
        m_aiDataMap = aiDataMap;

        int rowsTileset = imgTileset.getHeight() / tiledHeight;
        int columnsTileset = imgTileset.getWidth() / tiledWidth;
        m_aiTiledX = new int[rowsTileset * columnsTileset];
        m_aiTiledY = new int[rowsTileset * columnsTileset];

        int index = 0;
        for (int row = 0; row < rowsTileset; row++) {
            for (int column = 0; column < columnsTileset; column++) {
                m_aiTiledX[index] = column * m_iTiledWidth;
                m_aiTiledY[index] = row * m_iTiledHeight;
                index++;
            }
        }

    }
//    boolean change = false;
//
//    public void Render(Graphics g, int posX, int posY) {
//        //System.out.println("TILELAYER: " + m_iImageID); 
//        int row = posY / m_iTiledWidth; // Row in map
//        int column = posX / m_iTiledHeight; // Column in map
//        int rowLimit = 1 + row + Midlet.s_canGame.getHeight() / m_iTiledHeight;
//        int columnLimit = 1 + column + Midlet.s_canGame.getWidth() / m_iTiledWidth;
//
//        int indexData;
//        int valueTile;
//        for (int r = row; row >= 0 && row < m_iRowsMap && r < rowLimit; r++) {
//            for (int c = column; c >= 0 && c < m_iColumnsMap && c < columnLimit; c++) {
//                indexData = r * m_iColumnsMap + c;
//                if (indexData < m_aiDataMap.length) {
//                    valueTile = m_aiDataMap[indexData] - 1;
//                    if (valueTile == 48) {
//                        valueTile = (change) ? 48 : 49;
//                    }
//                    change = !change;
//                g.drawRegion(m_imgTileset, m_aiTiledX[valueTile], m_aiTiledY[valueTile], m_iTiledWidth, m_iTiledHeight, MySprite.TRANS_NONE, c * m_iTiledWidth - posX, r * m_iTiledHeight - posY, Graphics.TOP | Graphics.LEFT);
//
//                }
//                //System.out.print(valueTile + ",");
//            }
//            //System.out.println("");
//        }
//        //System.out.println("############");        
//    }

    public void Render(Graphics g, int posX, int posY) {
        //System.out.println("TILELAYER: " + m_iImageID); 
        int row = posY / m_iTiledWidth; // Row in map
        int column = posX / m_iTiledHeight; // Column in map
        int rowLimit = 1 + row + Midlet.s_canGame.getHeight() / m_iTiledHeight;
        int columnLimit = 1 + column + Midlet.s_canGame.getWidth() / m_iTiledWidth;

        int indexData;
        int valueTile;
        for (int r = row; r < rowLimit; r++) {
            for (int c = column; c < columnLimit; c++) {
                if(r < 0 || r >= m_iRowsMap || c < 0 || c >= m_iColumnsMap){
                   continue;
                }
                indexData = r * m_iColumnsMap + c;
                if (indexData >= 0 && indexData < m_aiDataMap.length) {
                    valueTile = m_aiDataMap[indexData] - 1;

                    if (valueTile >= 0 && valueTile < m_aiTiledX.length/* && valueTile < m_aiTiledY.length*/) {
                        g.drawRegion(m_imgTileset, m_aiTiledX[valueTile], m_aiTiledY[valueTile], m_iTiledWidth, m_iTiledHeight, DEFINE.TRANS_NONE, c * m_iTiledWidth - posX, r * m_iTiledHeight - posY, Graphics.TOP | Graphics.LEFT);
                    }

                }
            }
        }
    }
    
    public boolean Checkcollision(){
        return false;
    }

}
